package cn.snailhd.admin.novel.domain;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import cn.snailhd.utils.string.StringUtils;

public class CrawlerNovel {
	Log log = LogFactory.getLog(this.getClass());
	
	private String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
	private String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
	private String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
    
    
	private String id;
	private String chapterid;
	private String name;
	private String title;
	private String content;
	private String contentText;

	private String urlFixed;
	private String urlCur;
	private String urlPre;
	private String urlNext;
	
	private String email;
	private String titleSelector;
	private String titleClean;
	private String contentSelector;
	private String contentClean;
	private String nextSelector;
	private String nextEnd;
	private String preSelector;
	private String preEnd;
	private String fontSize = "24";
	private String fontColor;
	private String backgroundColor;
	
	private String enable;
	private String addTime;
	private String lastTime;
	private String userId;
	private String groupId;
	
	/**
	 * 请求数据并解析
	 * @throws IOException
	 */
	public CrawlerNovel parse() throws IOException {
		Document doc = Jsoup.connect(urlFixed+urlCur).get();
		
		//标题
		Element titleEle = doc.select(titleSelector).first();
		if(null!=titleEle) {
			String title= titleEle.text();
			if(StringUtils.isNotBlank(titleClean)) {
				String[] arr = titleClean.split("&&&&");
				for(String old :arr) {
					title = title.replace(old, "");
				}
			}
			this.title = title;
		}

		//内容
		Element contentEle = doc.select(contentSelector).first();
		String contentH="";
		if(contentEle!=null) {
			contentH=contentEle.toString();
			contentText = contentH.replaceAll(regEx_script, "")
					.replaceAll(regEx_style, "")
					.replaceAll(regEx_html, "")
					.replace("&nbsp;&nbsp;&nbsp;&nbsp;", "\r\n \t");
			if(StringUtils.isNotBlank(contentClean)) {
				String[] arr = contentClean.split("&&&&");
				for(String old :arr) {
					contentH = contentH.replace(old, "");
				}
			}
			log.info("---"+title+"---"+contentText.replace("\t", "").replace("\r", " ").substring(0,50).replaceAll("\n", " ").substring(0,20)+"......");
		};


		//下一页内容
		Element nextEle =doc.select(nextSelector).first();
		if(null!=nextEle) {
			String nextHref = nextEle.attr("href");
			if(nextHref.indexOf("/")>-1 ){
				nextHref = nextHref.substring(nextHref.lastIndexOf("/")+1);
			}
			this.urlNext = nextHref;
		}else {
			this.urlNext = nextEnd;
		}

		
		//上一页内容
		Element preEle =doc.select(preSelector).first();
		if(null!=preEle) {
			String preHref = preEle.attr("href");
			if(preHref.indexOf("/")>-1 ){
				preHref = preHref.substring(preHref.lastIndexOf("/")+1);
			}
			this.urlPre = preHref;
		}else {
			this.urlPre = preEnd;
		}

		
		
		/*String temp =   "	<div style=\" font-size: "+this.fontSize+"px;\">\r\n" +
						"		{@content} \r\n" + 
						"	</div>";*/
		String temp =  "{@content}";
		
		String html = temp.replace("{@content}", contentH);
		this.content = html;
		
		return this;

	}
	
	

	/**
	 * 下一页
	 * @throws IOException 
	 */
	public boolean next() throws IOException {
		if(!nextEnd.equals(this.urlNext)) {
			setUrlCur(this.urlNext);
			parse();
			return true;
		}else {
			return false;
		}
	}
	
	
	
	
	/**
	 * 上一页
	 * @throws IOException 
	 */
	public boolean pre() throws IOException {
		if(!preEnd.equals(this.urlPre)) {
			setUrlCur(urlPre);
			parse();
			return true;
		}else {
			return false;
		}
	}
	

	
	
	
	public void download(OutputStream os) throws IOException {
		parse();
		byte[] bytes = ("\r\n\r\n"+title+"\r\n\r\n"+contentText).getBytes();
		for(byte b:bytes) {
			os.write(b);
		}
		os.flush();
			
		while(next()) {
			bytes = ("\r\n\r\n"+title+"\r\n\r\n"+contentText).getBytes();
			for(byte b:bytes) {
				os.write(b);
			}
			os.flush();
		}
	}

	
	
	
	
	
	
	
	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getChapterid() {
		return chapterid;
	}

	public void setChapterid(String chapterid) {
		this.chapterid = chapterid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;

		this.contentText = content.replaceAll(regEx_script, "")
				.replaceAll(regEx_style, "")
				.replaceAll(regEx_html, "")
				.replace("&nbsp;&nbsp;&nbsp;&nbsp;", "\r\n \t");
	}

	public String getContentText() {
		return contentText;
	}

	public void setContentText(String contentText) {
		this.contentText = contentText;
	}

	public String getUrlFixed() {
		return urlFixed;
	}

	public void setUrlFixed(String urlFixed) {
		this.urlFixed = urlFixed;
	}

	public String getUrlCur() {
		return urlCur;
	}

	public void setUrlCur(String urlCur) {
		this.urlCur = urlCur;
	}

	public String getUrlPre() {
		return urlPre;
	}

	public void setUrlPre(String urlPre) {
		this.urlPre = urlPre;
	}

	public String getUrlNext() {
		return urlNext;
	}

	public void setUrlNext(String urlNext) {
		this.urlNext = urlNext;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTitleSelector() {
		return titleSelector;
	}

	public void setTitleSelector(String titleSelector) {
		this.titleSelector = titleSelector;
	}

	public String getTitleClean() {
		return titleClean;
	}

	public void setTitleClean(String titleClean) {
		this.titleClean = titleClean;
	}

	public String getContentSelector() {
		return contentSelector;
	}

	public void setContentSelector(String contentSelector) {
		this.contentSelector = contentSelector;
	}

	public String getContentClean() {
		return contentClean;
	}

	public void setContentClean(String contentClean) {
		this.contentClean = contentClean;
	}

	public String getNextSelector() {
		return nextSelector;
	}

	public void setNextSelector(String nextSelector) {
		this.nextSelector = nextSelector;
	}

	public String getNextEnd() {
		return nextEnd;
	}

	public void setNextEnd(String nextEnd) {
		this.nextEnd = nextEnd;
	}

	public String getPreSelector() {
		return preSelector;
	}

	public void setPreSelector(String preSelector) {
		this.preSelector = preSelector;
	}

	public String getPreEnd() {
		return preEnd;
	}

	public void setPreEnd(String preEnd) {
		this.preEnd = preEnd;
	}

	public String getFontSize() {
		return fontSize;
	}

	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}

	public String getFontColor() {
		return fontColor;
	}

	public void setFontColor(String fontColor) {
		this.fontColor = fontColor;
	}

	public String getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(String backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	public String getEnable() {
		return enable;
	}

	public void setEnable(String enable) {
		this.enable = enable;
	}

	public String getAddTime() {
		return addTime;
	}

	public void setAddTime(String addTime) {
		this.addTime = addTime;
	}

	public String getLastTime() {
		return lastTime;
	}

	public void setLastTime(String lastTime) {
		this.lastTime = lastTime;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}
	
}
